package com.example.myapplication.slice;

import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.components.element.StateElement;
import ohos.global.resource.NotExistException;
import ohos.global.resource.Resource;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Size;

import java.io.IOException;

public class CheckBoxAbilitySlice extends AbilitySlice {

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_checkbox);
        Checkbox cb1 = (Checkbox) findComponentById(ResourceTable.Id_cb1);
        Checkbox cb2 = (Checkbox) findComponentById(ResourceTable.Id_cb2);
        Checkbox cb3 = (Checkbox) findComponentById(ResourceTable.Id_cb3);
        cb1.setCheckedStateChangedListener(this::onCheckedChanged);
        cb2.setCheckedStateChangedListener(this::onCheckedChanged);
        cb3.setCheckedStateChangedListener(this::onCheckedChanged);
    }
    public void onCheckedChanged(AbsButton absButton, boolean b) {
        try {
            Resource resource = getContext().getResourceManager().getResource(ResourceTable.Media_checked);
            //获取设置背景元素对象
            PixelMapElement p1 = new PixelMapElement(resource);
            Resource resource2 = getContext().getResourceManager().getResource(ResourceTable.Media_unchecked);
            //获取设置背景元素对象
            PixelMapElement p2 = new PixelMapElement(resource2);

            //获取状态元素对象
            StateElement element = new StateElement();
            if (b) {
                element.addState(new int[]{ComponentState.COMPONENT_STATE_CHECKED},p1);
            }else{
                element.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY},p2);
            }
            //这个参数相对于被点击组件对象
            absButton.setButtonElement(element);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
